package com.rightsidetech.http.controller.admin.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.SysUserAddReq;
import com.rightsidetech.system.dtos.request.SysUserReq;
import com.rightsidetech.system.dtos.request.SysUserUpdateReq;
import com.rightsidetech.system.dtos.request.SysUserUpdateStatusReq;
import com.rightsidetech.system.dtos.response.SysRoleSelectResp;
import com.rightsidetech.system.dtos.response.SysUserResp;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.service.SysUserLoginInfoService;
import com.rightsidetech.system.service.SysUserService;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

/**
 * @Author: yhr
 * @Date: 2021-10-19 14:15
 */
@RestController
@RequestMapping("/admin/sys_user")
@ShenyuSpringMvcClient(path = "/admin/sys_user/**")
@Api(tags = "admin:后台用户")
public class SysUserController extends BaseController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserLoginInfoService sysUserLoginInfoService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/add")
    @ApiOperation("admin:新增后台账户")
    @ModifyLog(serviceClass = SysUserService.class,modifyType = ModifyName.SAVE)
    public ResponseData saveSyaAccount(@RequestBody SysUserAddReq sysUserAddReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(sysUserAddReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysUserService.saveSyaAccount(sysUserAddReq,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("admin:修改后台账户")
    @ModifyLog(serviceClass = SysUserService.class,idField = "loginInfoId")
    public ResponseData updateSysAccount(@RequestBody SysUserUpdateReq sysUserUpdateReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(sysUserUpdateReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysUserService.updateSysAccount(sysUserUpdateReq,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("admin:删除后台账号")
    @ModifyLog(serviceClass = SysUserService.class)
    public ResponseData deleteSysUserInfo(@RequestBody DelReq delReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(delReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysUserService.deleteSysUserInfo(delReq);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("admin:分页查询用户信息")
    public ResponseData<PageInfo<SysUserResp>> pageUserInfo(SysUserReq sysUserReq) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysUserService.pageUserInfo(sysUserReq,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update_status")
    @ApiOperation("admin:修改用户状态")
    @ModifyLog(serviceClass = SysUserService.class,idField = "loginInfoId")
    public ResponseData updateStatus(@RequestBody SysUserUpdateStatusReq sysUserUpdateStatusReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(sysUserUpdateStatusReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysUserService.updateStatus(sysUserUpdateStatusReq);
    }

    @Log
    @Token
    @GetMapping("/search")
    @ApiOperation("admin:员工姓名下拉框")
    public ResponseData<List<SysRoleSelectResp>> selectNameList() throws Exception {
        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        Long areaId = null;
        if(loginInfo.getSysCompanyAreaId() != 0){
            areaId = loginInfo.getSysCompanyAreaId();
        }

        return sysUserService.selectNameList(areaId);
    }
}
